package com.just521.xinshoutong.http;
import java.io.File;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.just521.xinshoutong.http.HttpHelperWraper.DownloadListener;



public class DlgDownloadListener implements DownloadListener {
	private ProgressDialog mProDlg;
	private boolean mbNeedShutdown=false;
	private Context mctx;
	DownloadListener mlistener;
	public DlgDownloadListener(Context ctx,boolean bNeedShutdown){
		mbNeedShutdown=bNeedShutdown;
		mctx=ctx;
	}
	public DlgDownloadListener(Context ctx,boolean bNeedShutdown,DownloadListener listener){
		mbNeedShutdown=bNeedShutdown;
		mctx=ctx;
		mlistener=listener;
	}
	public void onDownloadBegin(int total){
		mProDlg= new ProgressDialog(mctx);
		mProDlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//		mProDlg.setTitle("");
		mProDlg.setIndeterminate(false);
		mProDlg.setCancelable(false);
		mProDlg.setMax(total);
		mProDlg.show();
	}
	public void onDownloadUpdateProgress(int cur){
		mProDlg.setProgress(cur);
	}
	public void onDownloadEnd(String file){
		mProDlg.cancel();
		if(mlistener!=null){
			mlistener.onDownloadEnd(file);
		}else{
			
			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.setDataAndType(Uri.fromFile(new File(file)), "application/vnd.android.package-archive");
			mctx.startActivity(intent);
//		CommonDefine.gAppCtx.finish();
			if(mbNeedShutdown){
				
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		}
	}
	
}
